<template>
  <view>
    <!-- 搜索区域 -->
    <mySearch @click="goSearch"></mySearch>
    <!-- 一级菜单滚动条 -->
    <view class="scroll-view">
      <scroll-view class="left-scroll-view" :style="{height:wh + 'px'}" scroll-y>
        <view :class="['scroll-view-item', i === active ? 'active':' ' ]" v-for="(item,i) in cateList" :key="i" @click="activeChange(i)">{{item.cat_name}}</view>
      </scroll-view>
      <!-- 二级、三级 商品展示 -->
      <scroll-view class="right-scroll-view" :style="{height:wh + 'px'}" scroll-y :scroll-top="scrollTop">
        <view v-for="(item2,i2) in cateLevel2" :key="i2">
          <view class="cateLevel2">{{item2.cat_name}}</view>
          <view class="cateLevel3">
            <view class="cate-item" v-for="(item3,i3) in item2.children" :key="i3" @click="toShopList(item3.cat_id)">
              <image :src="item3.cat_icon.replace('dev','web')"></image>
              <text>{{item3.cat_name}}</text>
            </view>
          </view>
        </view>
      </scroll-view>
    </view>
  </view>
</template>

<script>
  import {getShopCateList} from '../../api/request.js'
  import mySearch from '../../components/mySearch/mySearch.vue'
import badge from '../../mixins/tabbar-badge.js'
  export default {
    mixins:[badge],
    components:{
      mySearch
    },
    data() {
      return {
        // 设备高度
        wh:0,
        // 激活项
        active:0,
        // 商品分类列表
        cateList:[],
        // 商品分类二级菜单
        cateLevel2:[],
        // 滚动条距离顶部距离
        scrollTop:0
      };
    },
    created(){
      this.getInfo()
      this.getNavList()
    },
    methods:{
      // 获取设备信息
      getInfo(){
        const info = uni.getSystemInfoSync()
        this.wh = info.windowHeight - 50
      },
      // 获取菜单项数据列表
      async getNavList(){
        const {data: res} = await getShopCateList()
        if(res.meta.status !== 200) return uni.$showMsg()
        this.cateList = res.message
        this.cateLevel2 = this.cateList[0].children
      },
      // 激活项改变
      activeChange(i){
        this.active = i
        this.cateLevel2 = this.cateList[i].children
        this.scrollTop = this.scrollTop === 0 ? 1:0
      },
      toShopList(id){
        uni.navigateTo({
          url: '/subpkg/goods_list/goods_list?cid=' + id
        })
      },
      // 跳转到搜索页面
      goSearch(){
        // console.log("ok");
        uni.navigateTo({
          url:'/subpkg/search/search'
        })
      }
      
    }
  }
</script>

<style lang="scss">
// 一级菜单滚动条
.scroll-view{
  display: flex;
  font-size: 14px;
  .left-scroll-view{
    width: 120px;
    background-color: #efefef;
    .scroll-view-item{
      line-height: 60px;
      width: 120px;
      text-align: center;
      position: relative;
      &.active{
        background-color: white;
        &::before{
          content: ' ';
          display: block;
          background-color: #1677ff;
          width: 3px;
          height: 55px;
          position: absolute;
          top: 50%;
          left: 0;
          transform: translateY(-50%);
        }
      }
    }
  }
  
}
.right-scroll-view{
  .cateLevel2{
      font-size: 12px;
      font-weight: bold;
      text-align: center;
      padding: 15px 0;
      border-bottom: 3px solid #1677ff ;
  }
  .cateLevel3{
      display: flex;
      flex-wrap: wrap;
    .cate-item{
          width: 33.33%;
          margin-bottom: 10px;
          display: flex;
          flex-direction: column;
          align-items: center;
      image{
        width: 60px;
        height: 60px;
      }
      text{
        font-size: 12px
      }
    }
  }
}

</style>
